package com.dxb.test.thread;

import java.util.concurrent.locks.ReentrantLock;

/**
 * ClassName：com.dxb.test.thread.LockABC <br>
 * Description：reentrantLock <br>
 *
 * @author 戴晓波 <br>
 * Date 2022/2/14/014 11:24 <br>
 * @version v1.0 <br>
 */
public class LockABC {
    private ReentrantLock lock = new ReentrantLock();
    private int num;

    public void print(int index) {
        for (int i = 0; i < 10; ) {
            lock.lock();
            try {
                if (num % 3 == index) {
                    num++;
                    i++;
                    System.out.println(Thread.currentThread().getName());
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        LockABC lockABC = new LockABC();
        new Thread(() -> lockABC.print(0), "A").start();
        new Thread(() -> lockABC.print(1), "B").start();
        new Thread(() -> lockABC.print(2), "C").start();
    }
}
